<?php
	require_once 'environment.php';
	$temp->addSubMenu("Schedule Us", "schedUs.php");
	if($acc->isLogged()) {
		$temp->addSubMenu("Add Event", "modEvent.php?action=addEvent");
		$temp->addSubMenu("Edit Event", "modEvent.php?action=editEvent");
		$temp->addSubMenu("Delete Event", "modEvent.php?action=delEvent");
		$temp->addSubMenu("Moderate Events", "modEvent.php?action=modEvent");
	}
	$temp->setTab(1,0);
	$temp->currentPage("Home >> Events >> Schedule Us");
	$temp->generate();
	
	$_event = new Event($acc, $db);
	
	function throwErr($what) {
		$what = urlencode($what);
		print change_site("schedUs.php?error={$what}");
		die();
	}
	
	if(!fixArr($_POST, "month")) {
		$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		print "<form action=\"\" method=\"POST\">\n";
		print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />" : "")."</span>";
		print "<table width=55% align=center><tr><td style=\"width:95px;\"><label>Event Name: </td>";
		print "<td><input type=\"text\" size=\"35\" maxlength=\"250\" name=\"eventName\" /></label></td></tr>";
		print "<tr><td>Your Name:</td>\n<td><input type=\"text\" size=\"35\" name=\"userName\" /></td>\n</tr>\n";
		print "<tr><td>Phone Number:</td>\n<td><input type=\"text\" size=\"35\" name=\"phone\" /></td>\n</tr>\n";
		print "<tr><td>Email:</td>\n<td><input type=\"text\" size=\"35\" name=\"email\" /></td>\n</tr>\n";
		print "<tr><td><label>Description: </td>";
		print "<td><input type=\"text\" maxlength=\"250\" size=\"35\" name=\"eventDesc\" /></label></td></tr>";
		print "<tr><td><label>Place: </td>";
		print "<td><input type=\"text\" maxlength=\"250\" size=\"35\" name=\"eventLoc\" /></label></td></tr>";
		print "<tr><td>Date: </td><td><select name=\"month\">\n";
		for($i = 0; $i < 12; $i++) print "<option".($months[$i] == date('F') ? " selected=\"selected\" " : "").">{$months[$i]}</option>\n";
		print "</select>\n";
		print "<select name=\"day\">\n";
		for($i = 1; $i <= 31; $i++) print "<option".($i == date('j') ? " selected=\"selected\" " : "").">{$i}</option>\n";
		print "</select>\n";
		print "<select name=\"year\">\n";
		for($i = date('Y'); $i <= date('Y') + 10; $i++) print "<option>{$i}</option>\n";
		print "</select></td></tr>";
		print "<tr><td>Time (HH:MM): </td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"1\" name=\"hour\" />&nbsp;:&nbsp;<input type=\"text\" maxlength=\"2\" size=\"1\" name=\"minute\" />\n<select name=\"ap\">\n<option>AM</option>\n<option>PM</option>\n</select>\n</td></tr>";
		print "<tr><td></td><td><br /><input type=\"submit\" value=\"Add Event\" /> <input type=\"reset\" value=\"Reset\" /></td></tr>";
		print "</table></form>";
	}else{
		foreach($_POST as $k => $data) {
			$_POST[$k] = trim($data);
			if(empty($_POST[$k]) && $k != "email") throwErr("addEvent", "A field was left empty. Please make sure all fields are filled out.");
		}
		$eventName = trim(fixArr($_POST, "eventName"));
		$eventDesc = trim(fixArr($_POST, "eventDesc"));
		$eventLoc = fixArr($_POST, "eventLoc");
		$month = fixArr($_POST, "month");
		$day = fixArr($_POST, "day");
		$year = fixArr($_POST, "year");
		$name = fixArr($_POST, "userName");
		$phone = fixArr($_POST, "phone");
		$email = fixArr($_POST, "email");
		if(!$email) $email = "";
		if(fixArr($_POST, "hour") > 12) throwErr("There are only 12 hours in a day/night. Select PM if you want to use evening times.");
		if(fixArr($_POST, "minute") > 59) throwErr("There are only 0-59 minutes in 1 hour.");
		$time = fixArr($_POST, "hour").":".fixArr($_POST, "minute")." ".fixArr($_POST, "ap");
		$eventTime = strtotime($month." ".$day.", ".$year." ".$time);
		if($eventTime < time()) throwErr("The date you picked is in the past.");
		$_event->submitEvent($eventName, $eventDesc, $eventTime, $eventLoc, $name, $phone, $email);
		print "Your event is now pending moderation!<br />\nCheck back every now and then to see if it was accepted!";
	}
?>